//
//  DutyListViewController.m
//  MobileOffice
//
//  Created by jjkj on 16/10/31.
//  Copyright © 2016年 liaoningjujin. All rights reserved.
//

#import "DutyListViewController.h"
#import "DutyHolidayTableViewCell.h"
#import "DutyListTableViewCell.h"
#import "OffDetailViewController.h"
#import "FamilyLeaveDetailViewController.h"
#import "InjuryDetailViewController.h"
#import "YearDetailViewController.h"
#import "MaternityDetailViewController.h"
#import "FuneralDetailViewController.h"
#import "MarriageDetailViewController.h"
#import "PersonalDetailViewController.h"
#import "SickDetailViewController.h"
#import "DutyDropListModel.h"
#import "PromptTimeView.h"
#import "DutyOvertimeViewController.h"
#import "GoOutApplyViewController.h"
#import "SignInAbnormalViewController.h"
#import "DutyGoBackViewController.h"

#define getDropList @"getDropList"

@interface DutyListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;//职责列表
    NSArray *_imageArr;//图片数组
    NSArray *_titleArr;//功能数组
    NSMutableDictionary *_dict;//保存请假和补假的开关状态
    //NSInteger _lastRow;//保存用户上次点击的cell
    DutyDropListModel *_dutyDropListModel;//功能列表
//    PromptTimeView *_promptTimeView;//请假时间提示框
//    NSInteger _dutyType;
}
@property (nonatomic,assign) NSInteger  lastRow;//保存用户上次点击的cell
@end

@implementation DutyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationTitle:@"职责"];
    [self setLeftBackNavigation];
    [self setRightBackNavigation];
    [self getDropListRequest];
    //[self creatView];
    // Do any additional setup after loading the view.
}

#pragma mark - creatTableView
- (void)creatView{
    _lastRow=-1;
    //初始化开关状态  默认为0 关闭  1 打开
    _dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"0",@"row0",@"0",@"row1", nil];
    NSArray *imageArr1=@[@"k_ic_qj@2x",@"k_ic_bj@2x",@"k_ic_kqyc@2x",@"k_ic_gc@2x",@"k_ic_jb@2x"];
    NSArray *imageArr2=@[@"k_ic_fqdrw@2x",@"k_ic_hysq@2x"];
    _imageArr=@[imageArr1,imageArr2];
    NSArray *titleArr1=@[@"请假",@"补假",@"考勤异常",@"公出",@"加班"];
    NSArray *titleArr2=@[@"发起任务",@"会议申请"];
    _titleArr=@[titleArr1,titleArr2];
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT-64) style:UITableViewStyleGrouped];
    //_myTableView.backgroundColor=[UIColor redColor];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.tag=110;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.showsHorizontalScrollIndicator=NO;
    _tableView.bounces=NO;
    //[self addTableViewUpdateRefresh:_tableView BindingIdentity:@""];
    //    [self addTableViewLoadMoreData:_tableView BindingIdentity:@""];
    [self.view addSubview:_tableView];
    
    //    self.definesPresentationContext = YES;
}

- (void)postViewControllerWithSelectDuty:(NSInteger)selectDuty dutyTypeStr:(NSString *)dutyTypeStr businessCode:(NSString *)businessCode selectRow:(NSInteger)selectRow{
    __weak DutyListViewController *mySelf=self;
    DutyDropListModel *model=_dutyDropListModel.dataArr[selectDuty];
    switch (selectDuty) {
        case 0:
        {
            OffDetailViewController *off=[OffDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 1:
        {
            FamilyLeaveDetailViewController *off=[FamilyLeaveDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 2:
        {
            InjuryDetailViewController *off=[InjuryDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 3:
        {
            YearDetailViewController *off=[YearDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 4:
        {
            MaternityDetailViewController *off=[MaternityDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 5:
        {
            FuneralDetailViewController *off=[FuneralDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 6:
        {
//            MarriageDetailViewController *off=[MarriageDetailViewController new];
//            off.navaTitle=[dutyTypeStr copy];
//            off.businessCode=[businessCode copy];
//            off.dutyType=selectRow;
//            off.keywordId=[model.keywordId copy];
//            [mySelf.navigationController pushViewController:off animated:YES];
            FuneralDetailViewController *off=[FuneralDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 7:
        {
            PersonalDetailViewController *off=[PersonalDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        case 8:
        {
            SickDetailViewController *off=[SickDetailViewController new];
            off.navaTitle=[dutyTypeStr copy];
            off.businessCode=[businessCode copy];
            off.dutyType=selectRow;
            off.keywordId=[model.keywordId copy];
            [mySelf.navigationController pushViewController:off animated:YES];
        }
            break;
        default:
            break;
    }

}



#pragma mark - TableViewDelagate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _titleArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_titleArr[section] count];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0&&(indexPath.row==0||indexPath.row==1)) {
        NSString *cellID=@"DutyHolidayTableViewCell";
        DutyHolidayTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell=[[DutyHolidayTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID row:indexPath.row+1 model:_dutyDropListModel viewWidth:IPHONE_WIDTH];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            __weak DutyListViewController *mySelf=self;
            cell.myBlock=^(NSInteger selectRow,NSInteger selectDuty,NSString *dutyTypeStr,NSString *businessCode){
                //根据保存的开关状态决定是否显示提示框
                if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"switch0"] boolValue]) {
//                    if (!_promptTimeView) {
                        PromptTimeView *promptTimeView=[[PromptTimeView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT) message:@"请假必须在40分钟内完成,确认进入申请页面吗?" prompt:@"知道了,下次不再提醒"];
                        //确定回调函数
                        promptTimeView.promptTimeBlock=^(){
                            [mySelf postViewControllerWithSelectDuty:selectDuty dutyTypeStr:dutyTypeStr businessCode:businessCode selectRow:mySelf.lastRow];
                        };
                        //取消回调函数
                        promptTimeView.promptTimeCancelBlock=^(){
                            //_promptTimeView=nil;
                        };
                        [mySelf.view addSubview:promptTimeView];
//                    }
                }else{
                    [mySelf postViewControllerWithSelectDuty:selectDuty dutyTypeStr:dutyTypeStr businessCode:businessCode selectRow:mySelf.lastRow];
                }
            };
        }
        if ([_dict[[NSString stringWithFormat:@"row%ld",(long)indexPath.row]] isEqualToString:@"0"]) {
            cell.lineLabel.y=63;
            cell.footView.hidden=YES;
        }else{
            cell.lineLabel.y=63+_dutyDropListModel.dataArr.count/3*(40+15)+15;
            cell.footView.hidden=NO;
        }
        [cell.headImageView setImage:[UIImage imageNamed:_imageArr[indexPath.section][indexPath.row]]];
        cell.myTitle.text=_titleArr[indexPath.section][indexPath.row];
        return cell;
    }
    NSString *cellID=@"DutyListTableViewCell";
    DutyListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell=[[DutyListTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell.headImageView setImage:[UIImage imageNamed:_imageArr[indexPath.section][indexPath.row]]];
    cell.myTitle.text=_titleArr[indexPath.section][indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0&&(indexPath.row==0||indexPath.row==1)) {
        if ([_dict[[NSString stringWithFormat:@"row%ld",(long)indexPath.row]] isEqualToString:@"0"]) {
            return 64;
        }else{
            return _dutyDropListModel.dataArr.count/3*(40+15)+15+64;
        }
    }
    return 64;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果是第一第二个cell
    if (indexPath.section==0&&(indexPath.row==0||indexPath.row==1)){
        if ([_dict[[NSString stringWithFormat:@"row%ld",(long)indexPath.row]] isEqualToString:@"0"]) {
            [_dict setObject:@"1" forKey:[NSString stringWithFormat:@"row%ld",(long)indexPath.row]];
            if (_lastRow!=indexPath.row) {
                [_dict setObject:@"0" forKey:[NSString stringWithFormat:@"row%ld",_lastRow]];
            }
            [_tableView reloadSections:[[NSIndexSet alloc]initWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }else{
            [_dict setObject:@"0" forKey:[NSString stringWithFormat:@"row%ld",(long)indexPath.row]];
            if (_lastRow!=indexPath.row) {
                [_dict setObject:@"1" forKey:[NSString stringWithFormat:@"row%ld",_lastRow]];
            }
            [_tableView reloadSections:[[NSIndexSet alloc]initWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        }
            _lastRow=indexPath.row;
    }else if (indexPath.section==0&&indexPath.row==4){
        DutyOvertimeViewController *overTime=[DutyOvertimeViewController new];
        overTime.navaTitle=@"加班";
//        overTime.businessCode=[businessCode copy];
//        overTime.dutyType=selectRow;
//        overTime.keywordId=[model.keywordId copy];
        [self.navigationController pushViewController:overTime animated:YES];
    }else if(indexPath.section==0&&indexPath.row==3){
        GoOutApplyViewController *goout=[GoOutApplyViewController new];
//        DutyGoBackViewController *goout=[DutyGoBackViewController new];
        goout.navaTitle=@"公出";
        [self.navigationController pushViewController:goout animated:YES];
    }else if (indexPath.section==0&&indexPath.row==2){
        SignInAbnormalViewController *goout=[SignInAbnormalViewController new];
        goout.navaTitle=@"考勤异常说明";
        [self.navigationController pushViewController:goout animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 46;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 46)];
    
    //蓝色条形
    UILabel *label=[MyControl creatLabelWithFrame:CGRectMake(10, 8, 8, 30) text:@""];
    label.layer.masksToBounds=YES;
    label.layer.cornerRadius=label.width/2;
    [backView addSubview:label];
    
    //常用软件
    UILabel *title=[MyControl creatLabelWithFrame:CGRectMake(26, 12, 80, 21) text:@""];
    title.font=FONT_17;
    if (section==0) {
        title.text=@"固定";
        label.backgroundColor=COLOR_SKYBLUE;
    }else{
        title.text=@"临时";
        label.backgroundColor=[UIColor orangeColor];
    }
    [backView addSubview:title];
    
    UILabel *lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 45, IPHONE_WIDTH, 1)];
    lineLabel.backgroundColor=BACKCOLOR_GRAY;
    [backView addSubview:lineLabel];
    return backView;
}

#pragma mark -获取功能请求

- (void)getDropListRequest{
    NSDictionary *dict=@{
                         @"dataSwith":@"1",
                         };
    
    //   字典转json
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString* str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    //拼接soap字符串
    NSString* soap = [NSString stringWithFormat:@"<sessionId>%@</sessionId>\n<content>%@</content>",[LZXHelper getSessionID],str];
    [LZXHelper postLoadViewWithText:HUD_PLEASEWAIT View:self.view];
    __weak DutyListViewController *mySelf=self;
    [HttpRequest SOAPData:[LZXHelper getServerURL] SoapMthod:getDropList soapBody:soap success:^(id responseObject) {
        [LZXHelper hideLoadViewWithView:self.view];
        
        //解析数据
        _dutyDropListModel=[[DutyDropListModel alloc]initWithResponse:responseObject];
        if ([_dutyDropListModel.code isEqualToString:@"200"]) {
            [mySelf creatView];
        }else if([_dutyDropListModel.code isEqualToString:@"220"]){
            [LZXHelper postMessageWithText:_dutyDropListModel.msg View:self.view];
        }
    } failure:^(NSError *error) {
       [LZXHelper hideLoadViewWithView:self.view];
    }];
}

////去掉UItableview headerview黏性
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    if (scrollView == _tableView)
//    {
//        CGFloat sectionHeaderHeight = 46;
//        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
//            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
//        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
//            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
//        }
//    }
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
